package com.leo.basic.scheduler.dao;

import com.leo.basic.scheduler.bean.SchedulerJob;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * @ClassName SchedulerJobSqlProvider
 * @Description
 * @Author wangss
 * @date 2021.09.23 16:20
 * @Version 1.0
 */
public class SchedulerJobSqlProvider {

    public String getScheduleJobByWhere(Map<String, Object> map) {
        SchedulerJob record = (SchedulerJob) map.get("record");
        StringBuilder sql = new StringBuilder();
        sql.append("select JOB_ID, CREATE_TIME, UPDATE_TIME, JOB_NAME, JOB_GROUP, JOB_STATUS, CRON_EXPRESSION,");
        sql.append("DESCRIPTION, BEAN_CLASS, IS_CONCURRENT, SPRING_ID, METHOD_NAME, PARENTJOBID, BATCH_STATUS, BATCH_TIME, TRIGGER_TYPE,");
        sql.append("PARAMETER_JSON, START_DATE, END_DATE from ");
        sql.append("${schedulerJObTableName}");

        if (record != null) {
            sql.append(" where 1=1");
            if (StringUtils.isNotEmpty(record.getJobName())) {
                sql.append(" AND JOB_NAME = #{record.jobName,jdbcType=VARCHAR}");
            }
            if (StringUtils.isNotEmpty(record.getCronExpression())) {
                sql.append(" AND CRON_EXPRESSION = #{record.cronExpression,jdbcType=VARCHAR}");
            }
        }
        sql.append(" order by CREATE_TIME");
        return sql.toString();
    }

    public String selectByPrimaryKey() {
        StringBuilder sql = new StringBuilder();
        sql.append("select JOB_ID, CREATE_TIME, UPDATE_TIME, JOB_NAME, JOB_GROUP, JOB_STATUS, CRON_EXPRESSION,");
        sql.append("DESCRIPTION, BEAN_CLASS, IS_CONCURRENT, SPRING_ID, METHOD_NAME, PARENTJOBID, BATCH_STATUS, BATCH_TIME, TRIGGER_TYPE,");
        sql.append("PARAMETER_JSON, START_DATE, END_DATE from ");
        sql.append("${schedulerJObTableName}");
        sql.append(" where JOB_ID=#{jobId,jdbcType=VARCHAR}");

        return sql.toString();
    }

    public String getAllScheduleJob(Map<String, Object> map) {
        SchedulerJob record = (SchedulerJob) map.get("record");
        StringBuilder sql = new StringBuilder();
        sql.append("select JOB_ID, CREATE_TIME, UPDATE_TIME, JOB_NAME, JOB_GROUP, JOB_STATUS, CRON_EXPRESSION,");
        sql.append("DESCRIPTION, BEAN_CLASS, IS_CONCURRENT, SPRING_ID, METHOD_NAME, PARENTJOBID, BATCH_STATUS, BATCH_TIME, TRIGGER_TYPE,");
        sql.append("PARAMETER_JSON, START_DATE, END_DATE from ");
        sql.append("${schedulerJObTableName}");
        if (StringUtils.isNotEmpty(record.getJobName())) {
            sql.append(" where JOB_NAME like '%");
            sql.append(" #{jobName,jdbcType=VARCHAR}%'");
        }
        return sql.toString();
    }
}
